package com.chen.base.module.excel.utils;

import com.chen.base.utils.SpringContextHolder;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author ChenJinLong
 */
public class ExcelInvokeHandler implements InvocationHandler {

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		if (Object.class.equals(method.getDeclaringClass())) {
			return method.invoke(this, args);
		}

//		Map<String, AbstractAchieve> beanMap = SpringContextHolder.getAppContext()
//				.getBeansOfType(AbstractAchieve.class);
//		for (AbstractAchieve achieve : beanMap.values()) {
//			if (achieve.name().equals(method.getName())){
//				return achieve.handler(args);
//			}
//		}

		throw new RuntimeException("未声明的函数对象: " + method.getName());
	}

	@SuppressWarnings("all")
	public static <T> T newExcelBaseMapperProxyInstance(Class<T> tc) {
		return (T) Proxy.newProxyInstance(tc.getClassLoader(), new Class[]{tc}, new ExcelInvokeHandler());
	}

}